package io.wdb.modules.generator.generator.service.impl.job;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.wdb.common.utils.DateFormatUtil;
import io.wdb.modules.generator.generator.dao.AccountCashLogDao;
import io.wdb.modules.generator.generator.dao.OrderTransDao;
import io.wdb.modules.generator.generator.dao.StatisticsDao;
import io.wdb.modules.generator.generator.dao.UserChargeLogDao;
import io.wdb.modules.generator.generator.dao.UserInfoDao;
import io.wdb.modules.generator.generator.entity.MetadataResponse;
import io.wdb.modules.generator.generator.entity.StatisticsEntity;
import io.wdb.modules.generator.generator.service.job.CapitalSchedulService;

@Service("capitalSchedulServiceImpl")
public class CapitalSchedulServiceImpl implements CapitalSchedulService{

	private Logger logger = LoggerFactory.getLogger(CapitalSchedulServiceImpl.class);
	
	@Autowired
	private StatisticsDao statisticsDao;
	
	@Autowired
	private UserChargeLogDao userChargeLogDao;
	
	@Autowired
	private UserInfoDao userInfoDao;
	
	@Autowired
	private OrderTransDao orderTransDao;
	
	@Override
	public void statisticalDespoint(Map<String,Object> params) {
		userChargeLogDao.statisticalDepositDay(params);
		
	}

	@Override
	public void statisticalMetadata(Map<String,Object> params) {
		
		Map<String,StatisticsEntity> map = new HashMap<String,StatisticsEntity>();
		
		//新增用户
		List<MetadataResponse> addUsers = userInfoDao.newUserCount(params);
		for(MetadataResponse metadataResponse:addUsers) {
			StatisticsEntity statisticsEntity = this.getStatisticsEntity(metadataResponse, map, params);
			statisticsEntity.setRegisterUse(metadataResponse.getValue());
		}
		//新增有效押金
		//BigDecimal addDeposit = userChargeLogDao.newDeposit(params);
		//statisticsEntity.setAvailableDeposit(addDeposit);
		//新增充值余额
		List<MetadataResponse> addBalances  = userChargeLogDao.newBalance(params);
		for(MetadataResponse metadataResponse:addBalances) {
			StatisticsEntity statisticsEntity = this.getStatisticsEntity(metadataResponse, map, params);
			statisticsEntity.setCashAmount(new BigDecimal(metadataResponse.getValue()));
		}
		//statisticsEntity.setCashAmount(addBalance);
		//出借书籍
		List<MetadataResponse> addBorrowBooks  = orderTransDao.newBorrowBooks(params);
		for(MetadataResponse metadataResponse:addBorrowBooks) {
			StatisticsEntity statisticsEntity = this.getStatisticsEntity(metadataResponse, map, params);
			statisticsEntity.setBorrowBooks(metadataResponse.getValue());
		}
		//statisticsEntity.setBorrowBooks(addBorrowBooks);
		//借阅用户
		List<MetadataResponse> addUserBorrow  = orderTransDao.newUserBorrows(params);
		for(MetadataResponse metadataResponse:addUserBorrow) {
			StatisticsEntity statisticsEntity = this.getStatisticsEntity(metadataResponse, map, params);
			statisticsEntity.setBorrowUser(metadataResponse.getValue());
		}
		//statisticsEntity.setBorrowUser(addUserBorrow);
		//新增有效押金用户
		
		//新增充值行为用户
		List<MetadataResponse> addBalanceUser = userChargeLogDao.newBalanceUser(params);
		for(MetadataResponse metadataResponse:addBalanceUser) {
			StatisticsEntity statisticsEntity = this.getStatisticsEntity(metadataResponse, map, params);
			statisticsEntity.setCashUser(metadataResponse.getValue());
		}
		//statisticsEntity.setCashUser(addBalanceUser);
		//退押金金额
		//BigDecimal addRefund = userChargeLogDao.newRefund(params);
		//statisticsEntity.setDepositAmount(addRefund);
		//活动支出
		//BigDecimal addActiveExpend = accountCashLogDao.newActiveExpend(params);
		//statisticsEntity.setActiveExpend(addActiveExpend);
		//
		logger.info("map中的数量为:"+map.size());
		if(map.size()<1) {
			StatisticsEntity statisticsEntity = new StatisticsEntity();
			statisticsEntity.setTime((Date) params.get("selectTime"));
			statisticsDao.save(statisticsEntity);
		}else {
			for(String key:map.keySet()) {
				statisticsDao.save(map.get(key));
			}
		}
	}
	/**
	 * 
	 * @Method:getStatisticsEntity
	 * @Description:从map中根据书柜号，获得每一个元数据
	 * @Time 2018年2月12日 下午3:58:05
	 * @author: Tom
	 * @param metadataResponse
	 * @param map
	 * @param params
	 * @return
	 */
	private StatisticsEntity getStatisticsEntity(MetadataResponse metadataResponse,Map<String,StatisticsEntity> map,Map<String,Object> params) {
		//for(MetadataResponse metadataResponse:addBalanceUser) {
			StatisticsEntity statisticsEntity = new StatisticsEntity();
			String time = DateFormatUtil.formateDateToString((Date)params.get("selectTime"), DateFormatUtil.DATE_PATTERN_LEN_HOUR);
			String key = time + metadataResponse.getBookShelfCode()==null?"":metadataResponse.getBookShelfCode();
			if(map.containsKey(key)) {
				statisticsEntity = map.get(key);
			}else {
				statisticsEntity.setBookShelfId(metadataResponse.getBookShelfCode()==null?"":metadataResponse.getBookShelfCode());
				statisticsEntity.setTime((Date)params.get("selectTime"));
				map.put(key, statisticsEntity);
			}
			return statisticsEntity;
		//}
	}
}
